package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import myJDBC.lowLevel.AttrObjectTypes;
import myJDBC.lowLevel.ObjectTypes;

@WebServlet("/SrvlCreateObjectType")
public class SrvlCreateObjectType extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if ((request.getParameter("name") == null) || (request.getParameter("parIdInt") == null) ) {
			missingParameters(request, response);
		} else {
			ArrayList<String> allParenAttrib = new ArrayList<String>();
			ArrayList<String> parAttr = new ArrayList<String>();
			String name = (String) request.getParameter("name");
			Integer parId= Integer.parseInt(request.getParameter("parIdInt"));
			String[] attributes= request.getParameterValues("attribute");
			if(parId!=-1){
				String[] tmparr= request.getParameterValues("parAttribute");
				for(String s: tmparr){
					parAttr.add(s);
				}
				ArrayList<String> tmp = ObjectTypes.returnObjectType(parId);						
				int barier= tmp.indexOf(null); 
				for (int i =barier+1; i < tmp.size(); i = i + 3) {
					allParenAttrib.add(tmp.get(i));
				}
			}
			int createdId=-1;
			try {
				createdId=ObjectTypes.createObjectType(name, parId);
			} catch (SQLException e) {
				e.printStackTrace();
			}if(createdId==-1){
				missingParameters(request, response);
			}else{	
				ArrayList<String> allAttributes= new ArrayList<String>();
				ArrayList<String> binds= new ArrayList<String>();
				if(attributes!=null)
					for(String s: attributes){
						allAttributes.add(s);
						binds.add(request.getParameter("bind"+s+""));
					}
				if((parId!=-1)&&(allParenAttrib.size()!=parAttr.size())){
					String unbind=Integer.toString(2);
					for(String s: allParenAttrib){
						if(!parAttr.contains(s)){
							allAttributes.add(s);
							binds.add(unbind);
						}
					}
				}
				try {
					AttrObjectTypes.createAttrObjectTypes(createdId,allAttributes,binds );
					response.sendRedirect("privateOffice");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}	
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private void missingParameters(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head>");
		out.println("<title>missing parameters</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>missing parameters</h1>");
		out.println("</body></html>");
		out.close();
	}
}
